import { _decorator, animation, Animation, AnimationClip } from "cc";
import { Entity } from "../Entity";
import { DirectionNames } from "../../enums/Direction";
import { CocosUtils } from "../../utils/CocosUtils";
const { ccclass } = _decorator;

@ccclass("IdleState")
export class IdleState extends animation.StateMachineComponent {

    public onMotionStateEnter(controller: animation.AnimationController, motionStateStatus: Readonly<animation.MotionStateStatus>): void {
        const entity = controller.getComponent(Entity);
        const direction = entity.type === Entity.Type.DOOR ? entity.direction % 2 : entity.direction;
        const entityName = entity.name;
        const animName = `${entityName}/idle/${DirectionNames[direction]}`;
        const atlasPath = `texture/${entityName}/idle/${DirectionNames[direction]}/idle`;
        const animation = controller.getComponentInChildren(Animation);
        CocosUtils.playAnimation(animation, animName, atlasPath, { wrapMode: AnimationClip.WrapMode.Loop });
    }
}